<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible" center fullscreen>
		<el-button class="miui-dialog-close-btn" @click="visible = false">关闭页面</el-button>
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="80px">
      <el-row>
        <el-col :span="6">
          <el-form-item label="仓位名称" prop="name">
            <el-input v-model="dataForm.name" placeholder="仓位名称"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="仓位编码" prop="code">
            <el-input v-model="dataForm.code" placeholder="仓位编码"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="所属公司" prop="unitId">
            <el-select v-model="dataForm.unitId" placeholder="请选择" @change="unitChangHandle">
              <el-option v-for="item in unitOptions" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="所属部门" prop="deptId">
            <el-select v-model="dataForm.deptId" placeholder="请选择">
              <el-option v-for="item in deptOptions" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="公司名称" prop="companyName">
            <el-input v-model="dataForm.companyName" placeholder="公司名称"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="社会统一代码" prop="taxId">
            <el-input v-model="dataForm.taxId" placeholder="社会统一代码"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="排序序号" prop="sort">
            <el-input v-model="dataForm.sort" placeholder="排序序号" type="number"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-row>
            <el-col :span="8">
              <el-form-item label="省">
                <el-select v-model="dataForm.provinceCode" filterable default-first-option clearable @change="getCityOptions">
                  <el-option v-for="item in provinceOptions" :value="item.id" :label="item.regionName" :key="item.id"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="市">
                <el-select v-model="dataForm.cityCode" filterable default-first-option clearable @change="getAreaOptions">
                  <el-option v-for="item in cityOptions" :value="item.id" :label="item.regionName" :key="item.id"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="区">
                <el-select v-model="dataForm.areaCode" filterable default-first-option clearable @change="areaChangeHandle">
                  <el-option v-for="item in areaOptions" :value="item.id" :label="item.regionName" :key="item.id"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
        </el-col>
        <el-col :span="12">
          <el-form-item label="详细地址" prop="address">
            <el-input v-model="dataForm.address" placeholder="详细地址"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="6">
          <el-form-item label="电话号码" prop="phone">
            <el-input v-model="dataForm.phone" placeholder="电话号码"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="手机号码" prop="mobilePhone">
            <el-input v-model="dataForm.mobilePhone" placeholder="手机号码"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit" :loading="ischeck" v-prevent-re-click>保 存</el-button>
    </span>
  </el-dialog>
</template>

<script>
    import { getStockObj, addStockObj, putStockObj } from '@/api/eims/stock'
    import { getDeptPage } from '@/api/admin/dept'
    import { unitList } from '@/api/admin/unit'
    import { getListByParentId } from '@/api/admin/region'
    export default {
    data () {
      return {
				ischeck: false,
        visible: false,
        deptOptions: [],
        unitOptions: [],
        provinceOptions: [],
        cityOptions: [],
        areaOptions: [],
        dataForm: {
          id: 0,
          name: '',
          code: '',
          companyName: '',
          provinceCode: '',
          province: '',
          cityCode: '',
          city: '',
          areaCode: '',
          area: '',
          address: '',
          taxId: '',
          unitId: '',
          deptId: '',
          sort: ''
        },
        dataRule: {
          name: [{ required: true, message: '名称不能为空', trigger: 'blur' }],
          code: [{ required: true, message: '编码不能为空', trigger: 'blur' }],
          unitId: [{ required: true, message: '单位id不能为空', trigger: 'blur' }],
          deptId: [{ required: true, message: '部门id不能为空', trigger: 'blur' }],
          sort: [{ required: true, message: '排序序号', trigger: 'blur' }]
        }
      }
    },
    // created () {
    //   this.getProvinceOptions()
    // },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.getCompanyList()
        this.getProvinceOptions()
        if(this.dataForm.unitId){
          let params = { current: 1, size: 100, unitId: this.dataForm.unitId }
          getDeptPage(params).then(response => {
            if(response.data.code == '0'){
              this.deptOptions = response.data.data.records
            }else{
              this.$message.error(response.data.msg)
            }
          })
        }
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            getStockObj(this.dataForm.id).then(response => {
              let resultData = response.data.data
              getListByParentId(resultData.provinceCode).then(response => {
                if(response.data.code == '0'){
                  this.cityOptions = response.data.data
                }else{
                  this.$message.error(response.data.msg)
                }
              })
              getListByParentId(resultData.cityCode).then(response => {
                if(response.data.code == '0'){
                  this.areaOptions = response.data.data
                }else{
                  this.$message.error(response.data.msg)
                }
              })
              resultData.provinceCode *= 1
              resultData.cityCode *= 1
              resultData.areaCode *= 1
              this.dataForm = resultData
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
				this.ischeck = true
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.dataForm.id) {
              putStockObj(this.dataForm).then(response => {
                if(response.data.code == '0'){
                  this.$message.success('修改成功')
                  this.visible = false
                  this.$emit('refreshDataList')
                }else{
                  this.$message.error(response.data.msg)
                }
                
              }).catch(() => {
                this.ischeck = false
              })
            } else {
              this.dataForm.status = '1'
              addStockObj(this.dataForm).then(response => {
                if(response.data.code == '0'){
                  this.$message.success('添加成功')
                  this.visible = false
                  this.$emit('refreshDataList')
                }else{
                  this.$message.error(response.data.msg)
                }
                this.ischeck = false
              }).catch(() => {
                this.ischeck = false
              })
            }
          }else{
						this.ischeck = false
					}
        })
      },
      //  获取公司列表1
      getCompanyList() {
        unitList().then(response => {
          if(response.data.code == '0'){
            this.unitOptions = response.data.data
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      unitChangHandle(val) {
        this.dataForm.deptId = ''
        let params = { current: 1, size: 100, unitId: val }
        getDeptPage(params).then(response => {
          if(response.data.code == '0'){
            this.deptOptions = response.data.data.records
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      // 获取省级地址列表
      getProvinceOptions(){
        var parentId = '86';
        getListByParentId(parentId).then(response => {
          if(response.data.code == '0'){
            this.provinceOptions = response.data.data
          }else{
            this.$message.error(response.data.msg)
          }
        }).catch(() => {
          alert("省级地址出错了，请联系管理员a ")
        })
      },
      // 获取市级地址列表
      getCityOptions(val){
        let provinceObj = this.provinceOptions.find(item => {
          return item.id == val
        })
        this.$set(this.dataForm,'province', provinceObj.regionName)
        this.cityOptions = []
        this.areaOptions = []
        this.$set(this.dataForm,'cityCode','')
        this.$set(this.dataForm,'areaCode','')
        if(val){
          getListByParentId(val).then(response => {
            if(response.data.code == '0'){
              this.cityOptions = response.data.data
            }else{
              this.$message.error(response.data.msg)
            }
          }).catch(() => {
            alert("市级地址出错了，请联系管理员a ")
          })
        }
      },
      // 获取区/县级地址列表
      getAreaOptions(val){
        let obj = this.cityOptions.find(item => {
          return item.id == val
        })
        this.$set(this.dataForm,'city', obj.regionName)
        this.areaOptions = []
        this.$set(this.dataForm,'areaCode','')
        if(val){
          getListByParentId(val).then(response => {
            if(response.data.code == '0'){
              this.areaOptions = response.data.data
            }else{
              this.$message.error(response.data.msg)
            }
          }).catch(() => {
            alert("市级地址出错了，请联系管理员a ")
          });
        }
      },
      areaChangeHandle(val){
        let obj = this.areaOptions.find(item => {
          return item.id == val
        })
        this.$set(this.dataForm,'area', obj.regionName)
      }
    }
  }
</script>
